package job;

import java.net.URL;
import java.sql.Connection;
import java.util.Date;
import java.util.ListIterator;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tools.DbTool;
import tools.HTTPCommonUtil;
import coreJob.IJob;
import coreJob.JobInfo;

public class ZyblJob implements IJob {
	private static Logger logger = LoggerFactory.getLogger(ZyblJob.class);

	public JobInfo setJobInfo() {
		
		 
		
		JobInfo job=new JobInfo();
		job.setJobGroup("zybl");//设置任务分组
		job.setJobName("抓取自己作业部落上的博客");//设置任务名称
		job.setStartTime(new Date()); //设置任务开始时间
		job.setTriggerType(0);//设置任务执行计划
		                      //1：每分钟执行一次,
		                      //2：每小时执行一次,
		                      //3：每天执行一次,
		                      //0：使用cron表达式执行，使用此项时cron表达式必填。
		job.setCron("0 0 23 * * ? *");
		job.setCronDescription("每天23：00执行一次");
		return job; 
	}
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		 
		
	}
	public static void main(String args[]){
		try{
		    URL url = new URL("https://www.zybuluo.com/ruoli/note/487030");
		    Document document=HTTPCommonUtil.getHttpsDocument(url, 15000);
		    ListIterator<Element> iterator= document.select("ul[id=file-list]")
		    .select("li>ul").select("li.file-item").select("a")
		    .listIterator();
		    Connection conn = DbTool.getConnection();
		    while(iterator.hasNext()){
		    	Element ulElement=iterator.next();
		    	insertOne(conn,ulElement.attr("href"));
		    	
		    }
		    conn.close();
		    
			}catch(Exception ex){
				
			}
	}
	
	public static void insertOne(Connection conn,String url){
		try{
			logger.info("开始处理："+url);
			 
			URL urls = new URL(url);
		    Document document=HTTPCommonUtil.getHttpsDocument(urls, 15000);
		    String content=document.select("div#editor-reader-full").toString();
		    String titile=document.select("h1").text();
		    String category=document.select("p[data-anchor-id]").first().select("code").text();
		    String id=document.select("span[class=whiter-on-black]").attr("id");
		    String source_url="https://www.zybuluo.com/ruoli/note/"+id;
		    
		    //判断此条记录是否存在
		    String isExsitSql="select count(1) from cms_zybl_article where id=?";
		    
		    
		    //插入新纪录
		    String sql="insert cms_zybl_article values(?,?,?,?,now(),now(),?,0)";
		    DbTool.executeUpdate(conn,sql, new Object[]{id,category,titile,content,source_url});
		    
		    logger.info("处理完成");
		   
			}catch(Exception ex){
				
			}
	}

}
